package com.cqjtu.store.cart.controller;


import com.cqjtu.store.cart.pojo.dto.AddressOrderDTO;
import com.cqjtu.store.cart.pojo.dto.CartDTO;
import com.cqjtu.store.cart.pojo.dto.ProductCartDTO;
import com.cqjtu.store.cart.pojo.vo.CartVO;
import com.cqjtu.store.cart.service.CartService;
import com.cqjtu.store.common.Constants;
import com.cqjtu.store.common.JwtUtils;
import com.cqjtu.store.common.R;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/cart")
@Slf4j
@Api(tags = "购物车模块")
public class CartController {
    @Autowired
    CartService cartService;

    /**
     * 自动解析token
     * @param request
     * @return
     */
    public UserLoginVO parseToken(HttpServletRequest request){
        //获取用户身份
        String token=request.getHeader(Constants.TOKEN_HEADER);
        UserLoginVO userLoginVO=JwtUtils.parseToken(token);
        return userLoginVO;
    }


    //查询购物车列表
    @GetMapping("list")
    @ApiOperation("查询购物车列表")
    public R<List<CartVO>> getCartList(HttpServletRequest request){
        //获取用户身份
        String token=request.getHeader("Authorization");
        UserLoginVO userLoginVO= JwtUtils.parseToken(token);
        //获取购物车列表
        List<CartVO> cartVOList=cartService.getCartList(userLoginVO);
        log.debug("cartVOList:{}",cartVOList);
        return R.ok(cartVOList);
    }
//修改购物车商品数量
    @PutMapping("updateNum")
    @ApiOperation("修改购物车商品数量")
    public R<List<CartVO>> updateNum(@RequestBody CartDTO cartDTO, HttpServletRequest request){
        //获取用户身份
        String token=request.getHeader("Authorization");
        UserLoginVO userLoginVO=JwtUtils.parseToken(token);
        List<CartVO> cartVOList=cartService.updateNum(userLoginVO,cartDTO);
        return R.ok(cartVOList);
    }
    //根据单个id删除购物车中的商品
    @DeleteMapping("delete/{id}")
    @ApiOperation("根据单个商品编号删除购物车商品")
    public R<Void> deleteCart(@PathVariable Integer id,HttpServletRequest request){
        //获取用户身份
        String token=request.getHeader("Authorization");
        UserLoginVO userLoginVO=JwtUtils.parseToken(token);
        cartService.deleteCart(userLoginVO,id);
        return R.ok();
    }
    //批量删除购物车中的商品
    @DeleteMapping("delete")
    @ApiOperation("批量删除购物车商品")
    public R<Void> deleteCarts(@RequestParam List<Integer> ids,HttpServletRequest request){
        //获取用户身份
        String token=request.getHeader("Authorization");
        UserLoginVO userLoginVO=JwtUtils.parseToken(token);
        cartService.deleteCarts(userLoginVO,ids);
        return R.ok();
    }

    //根据商品编号加入购物车
    @PostMapping("add/{id}")
    @ApiOperation("将商品加入购物车")
    public R<Void> addCart(@PathVariable Integer id,HttpServletRequest request){
        //获取用户身份
        String token=request.getHeader("Authorization");
        UserLoginVO userLoginVO=JwtUtils.parseToken(token);
        cartService.addCart(userLoginVO,id);
        return R.ok();
    }
    //根据商品数量将商品加入购物车
    @PostMapping("add")
    @ApiOperation("将多个商品加入购物车")
    public R<Void> addCarts(ProductCartDTO productCartDTO, HttpServletRequest request){
        //获取用户身份
        String token=request.getHeader("Authorization");
        UserLoginVO userLoginVO=JwtUtils.parseToken(token);
        cartService.addCarts(userLoginVO,productCartDTO);
        return R.ok();
    }

    //结算购物车
    @PostMapping("buy")
    @ApiOperation("购物车结算")
    public R<Integer> buy(@RequestBody List<CartDTO> cartDTOS,HttpServletRequest request){
        //获取用户身份
        String token=request.getHeader(Constants.TOKEN_HEADER);
        UserLoginVO userLoginVO=JwtUtils.parseToken(token);
        System.out.println(cartDTOS);
        Integer id=cartService.buy(userLoginVO,cartDTOS);
        return R.ok(id);
    }


    @PutMapping("pay")
    @ApiOperation("点击支付")
    public R<Void> pay(@RequestBody AddressOrderDTO addressOrderDTO,
                       HttpServletRequest request){
        //获取用户身份
        String token=request.getHeader(Constants.TOKEN_HEADER);
        UserLoginVO userLoginVO=JwtUtils.parseToken(token);
        this.cartService.pay(addressOrderDTO.getAid(), userLoginVO, addressOrderDTO.getOid());
        return R.ok();
    }

    @GetMapping("alreadyPay")
    @ApiOperation("完成支付")
    public R<Void> alreadyPay(@RequestParam("id") Integer id,
                       HttpServletRequest request){
        UserLoginVO userLoginVO = this.parseToken(request);
        this.cartService.alreadyPay(id);
        return R.ok();
    }



}
